/-docs
/-editor
/-files
/-files-old
/-imports
/-layout
/-storage ...
/-storage/attached ...
/-storage/attached/api
/-storage/attached/dom ...
DetectStorage.ts
LoadStorage.ts
UpdateStorage.ts
/-storage/attached/indexedDB
/-storage/attached/localStorage
/-storage/attached/webSQL
/-tests
/-typings
TypeScriptService.ts
functions.ts
ko.ts
persistence.api.ts
persistence.ts
shell.ts
teapo.html
teapo.js
teapo.ts
x
 
1
module teapo.storage.attached.dom {
2
3
  export class UpdateStorage {
4
5
    constructor(
6
      private _parentElement: HTMLElement,
7
      private _byName: { [fullPath: string]: HTMLElement; },
8
      private _document = document) {
9
    }
10
11
    update(file: string, property: string, value: string, callback?: (error: Error) => void): void {
12
13
      var element = this._getExistingElement(file);
14
15
      if (!element) {
16
        element = UpdateStorage.createElement(this._parentElement, file, this._document);
17
        this._byName[file] = element;
18
      }
19
20
      UpdateStorage.updateProperty(element, property, value);
21
22
      this._parentElement.setAttribute('editedUTC', Date.now() + '');
23
24
      callback(null);
25
    }
26
27
    remove(file: string, callback?: (error: Error) => void): void {
28
29
      var element = this._getExistingElement(file);
30
      if (!element) {
31
        callback(new Error('file does not exist.'));
32
        return;
33
      }
34
35
      element.parentElement.removeChild(element);
36
      delete this._byName[file];
37
38
      callback(null);
39
    }
40
41
    static createElement(parentElement: HTMLElement, fullPath: string, _document: typeof document): HTMLElement {
42
      var element = _document.createElement('script');
43
      element.setAttribute('data-path', fullPath);
44
      parentElement.appendChild(element);
45
      return element;
46
    }
47
48
    static updateProperty(element: HTMLElement, property: string, value: string): void {
49
      if (property)
50
        element.setAttribute('data-meta-' + property, value);
51
      else
52
        element.innerHTML = encodeForInnerHTML(value);
53
    }
54
55
    private _getExistingElement(fullPath): HTMLElement {
56
      var element = this._byName.hasOwnProperty(fullPath) ? this._byName[fullPath] : null;
57
      return element;
58
    }
59
60
  }
61
62
}
61:1